//
//  DECXChangePwdViewController.m
//  DECXInStore
//
//  Created by 程强 on 16/1/26.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DECXChangePwdViewController.h"
#import "DECXManager.h"
@interface DECXChangePwdViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) DECXManager *manager;
@end

@implementation DECXChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollHeight.constant = ScreenHeight;
    self.scrollWidth.constant = ScreenWidth;
    self.manager = [DECXManager new];
    
}
- (BOOL)checkInput
{
    if (self.oldPwdTf.text.length == 0) {
        ToastShowInWindow(@"请输入原始密码");
        return NO;
    }
    if (self.nPwdTf.text.length == 0) {
        ToastShowInWindow(@"请输入新密码");
        return NO;
    }
    if (![self.verifyPwdTf.text isEqualToString:self.nPwdTf.text]) {
        ToastShowInWindow(@"请确认新密码是否输入的一致");
        return NO;
    }
    return YES;
}
- (IBAction)commit:(id)sender
{
    if (![self checkInput]) {
        return;
    }else{
        [self resignKeyboard];
    }
    
    weak_Self(self);
    [self showHUD];
    [self performBlock:^{
        [weakSelf changeAction];
    } delay:0.5];
}

- (void)changeAction
{
    weak_Self(self);
    MyUser;
    [[self.manager changePwd:user.userid password:self.oldPwdTf.text newpassword:self.nPwdTf.text]subscribeNext:^(id x) {
        ToastShowInWindow(@"修改密码成功");
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        [weakSelf hideHUD];
    }];

}

- (void)resignKeyboard
{
    [self.oldPwdTf resignFirstResponder];
    [self.nPwdTf resignFirstResponder];
    [self.verifyPwdTf resignFirstResponder];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length == 0) {
        return YES;
    }
    
    BOOL suc = [NOValidateHelper validEnglish:toBeString];
    if (!suc) {
        return NO;
    }
    
    if (toBeString.length>=20) {
        textField.text = [toBeString substringToIndex: 20];
        return NO;
    }

    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignKeyboard];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
